Micron Document




Java syntax
part 31/46 · 86.7 KB total
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
void printReport(String header, int... numbers) { //numbers represents varargs
System.out.println(header);
for (int num : numbers) {
System.out.println(num);
}
}
// Calling varargs method
printReport("Report data", 74, 83, 25, 96);

Fields

Fields, or class variables, can be declared inside the class body to store data.

class Foo {
double bar;
}

Fields can be initialized directly when declared.

class Foo {
double bar = 2.3;
}

Modifiers

static - Makes the field a static member.
final - Allows the field to be initialized only once in a constructor or inside initialization block or during its declaration, whichever is earlier.
transient - Indicates that this field will not be stored during serialization.
volatile - If a field is declared volatile, it is ensured that all threads see a consistent value for the variable.

Inheritance

Classes in Java can only inherit from one class. A class can be derived from any class that is not marked as final. Inheritance is declared using the extends keyword. A class can reference itself using the this keyword and its direct superclass using the super keyword.

class Foo {
}
class Foobar extends Foo {
}

──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────